<template>
  <div class="role">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-button type="primary" icon="el-icon-plus" @click="dia_addrole=true">添加角色</el-button>
    <!-- 用户列表 -->
    <template>
      <el-table :data="RolerPowLists" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row v-for='first in scope.row.children' :key='first.id'>
              <el-col :span='4'>
                <el-tag closable type="success" @close="deleteRolesRight(scope.row,first.id)">
                  {{first.authName}}
                </el-tag>
              </el-col>
              <el-col :span='20'>
                <el-row v-for='second in first.children' :key='second.id'>
                  <el-col :span='4'>
                    <el-tag closable type="info" @close="deleteRolesRight(scope.row,second.id)">
                      {{second.authName}}
                    </el-tag>
                  </el-col>
                  <el-col :span='20'>
                    <el-tag closable type="warning" v-for='third in second.children' :key='third.id' @close="deleteRolesRight(scope.row,third.id)">
                      {{third.authName}}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- 没有数据权限则显示 -->
            <el-row v-if="scope.row.children.length === 0">
              <el-col style="text-align:center;font-size:15px;">
                该角色没有任何权限,请前往添加
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="#" prop="id">
        </el-table-column>
        <el-table-column label="角色名称" prop="roleName">
        </el-table-column>
        <el-table-column label="描述" prop="roleDesc">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑" placement="top">
              <el-button type="success" icon="el-icon-edit" @click="getEditRoleRight(scope.row),dia_editrole=true" plain></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top">
              <el-button type="success" icon="el-icon-delete" @click="handleDelRole(scope.row)" plain></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="权限分配" placement="top">
              <el-button type="success" icon="el-icon-check" @click="getRolePow(scope.row),dia_roleTree=true" plain></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!-- 模态框----添加角色 -->
    <el-dialog title="添加角色" :visible.sync="dia_addrole" width="30%" :before-close="handleClose">
      <el-form :label-position="labelPosition" label-width="80px" :model="addForm" ref="addForm" :rules="rules">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="addForm.roleName" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="addForm.roleDesc" placeholder="请描述该角色"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAddRole">确认添加</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 模态框----编辑角色 -->
    <el-dialog title="编辑角色" :visible.sync="dia_editrole" width="30%" :before-close="handleClose">
      <el-form :label-position="labelPosition" label-width="80px" :model="editRoleArr" ref="editRoleArr" :rules="rules">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="editRoleArr.roleName" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="editRoleArr.roleDesc" placeholder="请描述该角色"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleEditRole">确认修改</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 模态框----添加权限 -->
    <el-dialog title="分配权限" :visible.sync="dia_roleTree">
      <div class="treeBox" style="height:400px;overflow:auto">
        <el-tree ref="tree" :data="addRolePow" :default-expand-all="true" show-checkbox node-key="id" :default-checked-keys="checkArr" :props="defaultProps">
        </el-tree>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRolePow(),dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRolerPowData,
  getPowerData,
  addRole,
  delRole,
  grantroleright,
  deleterolerightById,
  editRoleRight
} from "@/api/index.js";
export default {
  data() {
    return {
      roleId: "",
      RolerPowLists: [],
      addRolePow: [],
      checkArr: [],
      editRoleArr: {
        roleDesc: "",
        roleName: "",
        id: 1
      },
      defaultProps: {
        label: "authName",
        children: "children"
      },
      dia_addrole: false,
      dia_roleTree: false,
      dia_editrole: false,
      //表单对齐方式
      labelPosition: "left",
      addForm: {
        roleName: "",
        roleDesc: ""
      },
      //表单验证
      rules: {
        roleName: [
          { required: true, message: "角色名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  mounted() {
    this.showRolePowData();
  },
  methods: {
    handleClose(done) {
      done();
    },
    showRolePowData() {
      getRolerPowData().then(res => {
        console.log(res);
        this.RolerPowLists = res.data;
      });
    },
    handleAddRole() {
      if (this.addForm.roleName.trim() == "") {
        this.$notify({
          title: "标题名称",
          message: h("i", { style: "color: teal" }, "角色名称不能为空")
        });
        return;
      } else {
        console.log(this.addForm);
        addRole(this.addForm).then(res => {
          if (res.meta.status === 201) {
            // console.log(res);
            this.$message({
              message: res.meta.msg,
              type: "success"
            });
            //弹窗关闭
            this.dia_addrole = false;
            //更新数据
            this.showRolePowData();
          }
        });
      }
    },
    handleDelRole(row) {
      console.log(row);
      this.$confirm("是否删除该用户?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          delRole(row.id).then(res => {
            //   console.log(res)
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.showRolePowData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    getRolePow(row) {
      // this.id = row.id
      //加载所有权限数据
      getPowerData("tree").then(res => {
        this.addRolePow = res.data;
      });
      //   console.log(row);
      //将当前的角色id 存储到 data属性中
      this.roleId = row.id;

      //遍历当前角色的权限数组
      if (row.children.length > 0) {
        row.children.forEach(first => {
          if (first.children.length > 0) {
            first.children.forEach(second => {
              if (second.children.length > 0) {
                second.children.forEach(third => {
                  this.checkArr.push(third.id);
                });
              }
            });
          }
        });
      }
    },
    //给角色授权
    setRolePow() {
      var nodeArr = this.$refs.tree.getCheckedNodes();
      // console.log(nodeArr);
      var idArr = nodeArr.map(value => {
        return value.id + "," + value.pid;
      });
      // console.log(idArr);
      var strArr = idArr.join(",");
      // console.log(strArr);
      var ridsArr = strArr.split(",");
      // console.log(ridsArr);
      var tempSet = new Set(ridsArr);
      var final = Array.from(tempSet);
      // console.log(final.join(","));
      var rids = final.join(",");
      //调用接口 为角色授权
      grantroleright({ roleid: this.roleId, rids: rids }).then(res => {
        if (res.meta.status === 200) {
          this.$message({
            message: res.meta.msg,
            type: "success"
          });
          // console.log(res);
          this.dia_roleTree = false;
          this.showRolePowData();
        } else {
          this.$message({
            message: res.meta.msg,
            type: "error"
          });
        }
      });
    },
    //删除指定id
    deleteRolesRight(row, rightid) {
      deleterolerightById(row.id, rightid).then(res => {
        console.log(res);
        if (res.meta.status === 200) {
          this.$message({
            message: res.meta.msg,
            type: "success"
          });
          row.children = res.data;
        }
      });
    },
    //编辑角色内容
    getEditRoleRight(row) {
      // console.log(row)
      this.editRoleArr.roleDesc = row.roleDesc;
      this.editRoleArr.roleName = row.roleName;
      this.editRoleArr.id = row.id;
      // console.log(this.editRoleArr)
    },
    handleEditRole() {
      // console.log(formname)
      editRoleRight(this.editRoleArr.id, this.editRoleArr).then(res => {
        console.log(res)        
        this.$message({
          message: "编辑成功",
          type: "success"
        })
        //关闭弹窗
        this.dia_editrole = false
        //更新数据
        this.showRolePowData()
      });
    }
  }
};
</script>

<style scoped>
.el-row .el-col {
  margin-bottom: 10px;
}
.el-tag {
  margin: 5px 10px;
}
</style>
